$(document).ready(function(){
	common.body_to_all();
	index_js.init();
	$("#page_title").html("我的医生");
});
var index_js = {};
index_js.init = function(){
	vm.load();
}
var vm = avalon.define({
	$id:"main_controller",
	//----------------
	//toast的信息
	toast_msg:'',
	//是否要展示toast
	is_toast_show:false,
	//定时器
	$timeout:0,
	//消息隐藏
	clear_toast:function(){
		//设置定时器
		if(vm.$timeout){
			clearTimeout(vm.$timeout);
		}
		vm.$timeout = setTimeout(function(){
			vm.is_toast_show = false;
			vm.toast_msg = '';
		},3000);		
	},	
	//-----------------
	//集合
	list:[],
	//加载数据
	load:function(){
		vm.is_loading = true;
		$.ajax({
			type: "post",
			url: get_member_doctor_url,
			dataType: 'json',
			success: function(data) {
				console.log(data);
				vm.list = data;
				//执行按需加载
				setTimeout(function(){
					$(".doctor-img").unveil();
					vm.is_loading = false;
				},100);					
				vm.is_loading = false;
			},
			error: function() {
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();
			},
			data:"status="+common.getUrlParam('type'),
		});			
	},
	is_loading:false,
	/**
	 * 跳转到首页
	 * @param {Object} index
	 */
	go_to_index:function(index){
		location.href = home_url+"?id="+vm.list[index].dietitianid+"&flag=true";
	},
	//要进行删除的ID
	memanddietitianid:0,
	doctor:'',
	//点击弹出删除确认框
	show_delete:function(index){
		vm.memanddietitianid = vm.list[index].memanddietitianid;
		vm.doctor = vm.list[index].echoname;
		$("#myModal").modal('show');
	},
	//取消删除
	cancel_delete:function(){
		$("#myModal").modal('hide');
	},
	//提交删除
	submit_delete:function(){
		var $this = $(this);
		$this.button('loading');
		var fd = new FormData();
		fd.append('memanddietitianid',vm.memanddietitianid)
		$.ajax({
			type:"post",
			url:delete_member_doctor_url,
			dataType:"json",
			data:fd,
			processData: false,
			contentType: false,					
			success:function(data){
				$this.button('reset');
				$("#myModal").modal('hide');
				if(data.status == 1){
					vm.toast_msg = data.msg;
					vm.load();
				}else if(data.status == 2){
					vm.toast_msg = data.msg;
				}else{
					vm.toast_msg = data.msg;
				}
				vm.is_toast_show = true;
				vm.clear_toast();
			},
			error:function(){
				$this.button('reset');
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},	
		});		
	},	
});
avalon.scan();
avalon.ready(function(){
	$('body').css('opacity',1);
});
